package com.iqismart.shorturl.config;

import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @Description
 * @Auther sty
 * @createTime 2019/10/14 01:47
 */
@Configuration
public class JSONWebMvcConfigurerAdapter implements WebMvcConfigurer {
    public JSONWebMvcConfigurerAdapter() {
    }

    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(new SerializerFeature[]{SerializerFeature.PrettyFormat});
        fastJsonConfig.setSerializeFilters(new SerializeFilter[]{new PropertyFilter() {
            public boolean apply(Object object, String name, Object value) {
                if (value == null) {
                    return false;
                } else {
                    return !(value instanceof String) || !StringUtils.isEmpty((CharSequence)value);
                }
            }
        }});
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastConverter);
    }
}
